/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "ola_log.h"
#include <stdarg.h>
#include <stdio.h>
#include <stddef.h>
#include "ola_error.h"
#include "ola_pal_os.h"

static int g_logLevel = OLA_LOG_DEFAULT_LEVEL;

int OlaLogLevelReset(void)
{
    g_logLevel = OLA_LOG_DEFAULT_LEVEL;
    return OLA_OK;
}

int OlaSetLogLevel(int level)
{
    if (level < OLA_LOG_INER_LEVEL_MIN || level > OLA_LOG_INER_LEVEL_DEBUG) {
        OlaLogWarning("log level invalid");
        return OLA_ERROR;
    }
    OlaLogNotice("set log level[%d]", level);
    g_logLevel = level;
    return OLA_OK;
}

bool OlaIsLogLevelPrint(int level)
{
    if (level < OLA_LOG_INER_LEVEL_MIN || level > g_logLevel) {
        return false;
    }
    return true;
}

int OlaGetLogLevel(void)
{
    return g_logLevel;
}

void OlaDumpChar(const char *str, unsigned int len)
{
    if ((str == NULL) || (len == 0)) {
        return;
    }
    for (unsigned int i = 0; i < len; ++i) {
        OlaPrintf("%c", str[i]);
    }
}

void OlaDumpHex(const unsigned char *data, unsigned int len)
{
    if ((data == NULL) || (len == 0)) {
        return;
    }
    for (unsigned int i = 0; i < len; ++i) {
        OlaPrintf("%02x", data[i]);
    }
}